<template>
    <div>
      <div class="title">
        <h3>我的订单</h3>
      </div>
      <ul class="nav">
        <li class="item" :class="tabIndex==0 ? 'active':''" @click="toggleNav(0)">全部订单</li>
        <li class="item" :class="tabIndex==1 ? 'active':''" @click="toggleNav(1)">待付款</li>
        <li class="item" :class="tabIndex==2 ? 'active':''" @click="toggleNav(2)">待发货</li>
        <li class="item" :class="tabIndex==3 ? 'active':''" @click="toggleNav(3)">待收货</li>
      </ul>
      <div class="mcon">
        <ul>
          <li class="mitem" v-for="item in orderAllItem[tabIndex]">
            <h3>订单号: {{item.orderNum}} <span>{{item.itemName}}</span></h3>
            <div class="infobox" @click="$router.push({path: '/my-order-detail',query: {id: item.oid}})">
              <img :src="item.imgSrc" alt="">
              <div class="info">
                <h4>{{item.proName}}</h4>
                <p>￥<span>{{item.price}}</span></p>
                <span>x{{item.count}}</span>
              </div>
            </div>
            <p class="kd" v-show="item.status == 2 || item.status == 3">
              <span>物流号: {{item.trackNum}}</span>
              <button
              v-clipboard:copy="item.trackNum"
              v-clipboard:success="onCopy"
              >复制</button>
            </p>
            <p class="kd" v-show="item.status==1">
              <button @click="payNow(item.oid)">立即付款</button>
            </p>
            <!-- <p class="kd" v-show="item.status==0">
              <button>再来一单</button>
            </p> -->
          </li>
          <li v-show="orderAllItem[tabIndex].length==0">
            <p class="noorder">您还没有订单！</p>
          </li>
        </ul>
      </div>
    </div>
</template>

<script>
import Order from '../api/order/order';
import utils from '../utils/util';
export default {
  name: 'my-order',
  data() {
    return {
      orderAllItem: [
        [],
        [],
        [],
        []
      ],
      tabIndex: 0
    };
  },
  methods: {
    toggleNav(i) {
      this.tabIndex = i;
    },

    async initOrders(i=0) {
      let orders = await Order.getOrdersList();
      console.log('foo:', orders);

      orders = orders.map(o => Order.orderAdaptor(o));

      // let orders = [];    //全部订单
      let completesItem = [];   //已完成订单  status:0
      let paymentsItem = [];   //待付款  status: 1
      let sendsItem = [];    //待发货  status:2
      let receiptsItem = [];   //待收货  status:3

      //orders虚拟数据
      // orders = [
        // {
        //   oid: 0,
        //   status: 1,
        //   itemName: '待付款',
        //   orderNum: '201903272345345',  //订单号
        //   proName: 'iPhone XS全面屏智能学生老人拍照手机全面屏智能学生老人拍照手',  //产品名称
        //   imgSrc: '../../static/images/1.jpg',
        //   price: 6565.00,
        //   count: 2,
        //   trackNum: '21345323',   //快递单号
        // },
        // {
        //   oid: 1,
        //   status: 0,
        //   itemName: '交易完成',
        //   orderNum: '201903272345345',  //订单号
        //   proName: 'iPhone XS全面屏智能学生老人拍照手机全面屏智能学生老人拍照手',  //产品名称
        //   imgSrc: '../../static/images/1.jpg',
        //   price: 6565.00,
        //   count: 1,
        //   trackNum: '21345323',   //快递单号
        // },
        //

      // ];

      //把所有不同状态的订单通过if判断push到相对应的订单状态集合中
      orders.forEach((order) => {
        if(order.status == 1){
          paymentsItem.push(order);
        }
        if(order.status == 2){
          sendsItem.push(order);
        }
        if(order.status == 3){
          receiptsItem.push(order)
        }
      });

      let orderAll = [orders, paymentsItem, sendsItem, receiptsItem];
      this.orderAllItem = orderAll;
      this.toggleNav(i);
    },

    async payNow(orderId) {
      Order.payForOrder(orderId, res => {
        this.initOrders(2);
      });
    },

    onCopy() {
      alert('复制成功');
    },


  },

  mounted() {
    // this.tabIndex = this.$route.query.id;
    utils.wxPayHrefReload();
    this.initOrders();
  }
}
</script>

<style scoped>
.title {
  height: 1.4rem;
  -webkit-box-shadow: 0 1px 3px #cddcdc;
  -moz-box-shadow: 0 1px 3px #cddcdc;
  box-shadow: 0 1px 3px #cddcdc;
}
.title h3{
  height: 1.4rem;
  line-height: 1.4rem;
  text-align: center;
  color: #000;
  font-size: .42rem;
}
.nav{
  padding: 0 .6rem;
  display: flex;
  display: -webkit-flex;
  justify-content: space-between;
  align-items: center;
  height: 1.2rem;
  border-bottom: 1px solid #e1e1e1;
}
.item{
  font-size: .37rem;
  color: #333;
}
.item.active{
  color: #f00;
}
.mcon{
  border-top: .2rem solid #f1f1f1;
  margin-bottom: 1rem;
}
.mitem{
  border-bottom: .2rem solid #f1f1f1;
  padding: .3rem;
}
.mitem>h3{
  height: .6rem;
  line-height: .6rem;
  /*border-bottom: 1px solid #e2e2e2;*/
  font-size: .35rem;
}
.mitem>h3 span{
  color: #f00;
  float: right;
  font-size: .36rem;
}
.infobox{
  display: flex;
  display: -webkit-flex;
  justify-content: space-between;
  padding: .3rem 0;
}
.infobox img{
  display: block;
  width: 2.2rem;
  height: 2.2rem;
  margin-right: .3rem;
  flex-shrink: 0;
  border: 1px solid #c6c6c6;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
}
.infobox .info{
  flex-grow: 1;
}
.infobox .info h4{
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  height: 1.04rem;
  line-height: .52rem;
  font-size: .38rem;
  color: #333;
}
.infobox .info p{
  color: #f00;
  font-size: .34rem;
  margin-top: .1rem;
}
.infobox .info p span{
  font-size: .42rem;
  color: #f00;
}
.infobox .info span{
  font-size: .34rem;
  color: #555;
}
.mcon p.noorder{
  font-size: .4rem;
  text-align: center;
  color: #555;
  margin: 2rem auto;
}
.mitem .kd{
  font-size: .35rem;
  color: #555;
}
.mitem button{
  display: inline-block;
  padding: 0 .2rem;
  height: .6rem;
  line-height: .6rem;
  border: 1px solid #c1c1c1;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 8px;
  font-size: .32rem;
  background: #fff;
  margin-left: .2rem;
  color: #666;
}
</style>
